/*
 * Copyright the original author or authors.
 * 
 * Licensed under the MOZILLA PUBLIC LICENSE, Version 1.1 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *	  http://www.mozilla.org/MPL/MPL-1.1.html
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.googlecode.milib.dbg.model.dbgservice {
	import com.googlecode.milib.dbg.MLDBG;
	import com.googlecode.milib.dbg.model.content.MLDBGClassMethodsListModel;
	import com.googlecode.milib.dbg.model.content.MLDBGClassVariablesListModel;
	import com.googlecode.milib.dbg.model.content.MLDBGLoggerModel;
	import com.googlecode.milib.dbg.model.content.MLDBGValuesListModel;
	import com.googlecode.milib.dbg.model.dbgservice.AbstractDBGService;
	import com.googlecode.milib.dbg.view.content.MLDBGTextView;
	import com.googlecode.milib.dbg.view.content.MLDBGValuesListView;

	import flash.utils.Dictionary;

	/**
	 * @author Marek Brun
	 */
	public class DBGServiceForClass extends AbstractDBGService {
		
		private static const servicedObjects : Dictionary = new Dictionary(true);
		private var logModel:MLDBGLoggerModel;
		private var mldbg:MLDBG;
		private var variablesListModel:MLDBGClassVariablesListModel;
		private var variablesListView:MLDBGValuesListView;
		private var valuesModel:MLDBGValuesListModel;
		private var methodsListModel:MLDBGClassMethodsListModel;
		private var methodsListView:MLDBGValuesListView;
		private var dbgContextIDs:Dictionary;
		
		public function DBGServiceForClass(access:Private, obj:Object) {
			super(obj);
			logModel=new MLDBGLoggerModel();
			valuesModel=new MLDBGValuesListModel();
			mldbg=MLDBG.getInstance();
		}
		
		override public function getMLDBGWindowTitle():String {
			return MLDBG.getClassNameByValue(obj);
		}
		
		override public function getMLDBGWindowContents():Array {
			return [
				createVariablesView(),
				createMethodsView(),
				createLogView(),
				createValuesView()
			];
		}
		
		public function createValuesView():MLDBGValuesListView {
			var valuesView:MLDBGValuesListView=new MLDBGValuesListView('vlogs');
			valuesView.setModel(valuesModel);
			return valuesView;
		}
		
		public function createLogView():MLDBGTextView {
			var logView:MLDBGTextView=new MLDBGTextView('logs');
			logView.setModel(logModel);
			return logView;
		}
		
		protected function createVariablesView():MLDBGValuesListView {
			if(!variablesListModel){
				variablesListModel=new MLDBGClassVariablesListModel(obj);
			}
			variablesListView=new MLDBGValuesListView('variables');
			variablesListView.setModel(variablesListModel);
			return variablesListView;
		}
		
		protected function createMethodsView():MLDBGValuesListView {
			if(!methodsListModel){
				methodsListModel=new MLDBGClassMethodsListModel(obj);
			}
			methodsListView=new MLDBGValuesListView('methods');
			methodsListView.setModel(methodsListModel);
			return methodsListView;
		}
		
		public function log(text:String, dbgContextID:*=null):void {
			if(dbgContextID==null){
				mldbg.addLogWithTitle(mldbg.getLink(obj), text);
				logModel.addLog(text);
			}else{
				if(gotDBGContext(dbgContextID)){
					mldbg.addLogWithTitle(mldbg.getLink(obj)+' context:'+mldbg.getLink(dbgContextID), text);
					logModel.addLog('context:'+mldbg.getLink(dbgContextID)+'> '+text);
				}
			}
		}
		
		public function logLocal(text:String):void {
			logModel.addLog(text);
		}
		
		public function logv(name:*, data:*=null, dbgContextID:*=null):void {
			if(dbgContextID==null){
				mldbg.addValueLog(name, data);
				valuesModel.addValue(name, data);
			}else{
				if(gotDBGContext(dbgContextID)){
					mldbg.addValueLog('context:'+mldbg.getLink(dbgContextID)+'>'+name, data);
					valuesModel.addValue('context:'+mldbg.getLink(dbgContextID)+'>'+name, data);
				}
			}
		}
		
		public function logFPSTag(text:String):void {
			mldbg.addFpsMonitorTag(text);
		}
		
		public function logMemoryTag(text:String):void {
			mldbg.addMemoryMonitorTag(text);
		}
		
		public function link(value:*, isRemember:Boolean=false):String {
			return MLDBG.getInstance().getLink(value, true, isRemember);
		}
		
		public function startSpeedTest(name:String):void {
			MLDBG.getInstance().addStartSpeedTest(name);
		}
		
		public function finishSpeedTest(name:String):void {
			MLDBG.getInstance().addFinishSpeedTest(name);
		}
		
		public static function forInstance(obj:Object):DBGServiceForClass {
			if(servicedObjects[obj]){
				return servicedObjects[obj];
			}else{
				servicedObjects[obj]=new DBGServiceForClass(null, obj);
				return servicedObjects[obj];
			}
		}
		
		public function registerAtDBGContext(dbgContextID:*):void {
			if(!dbgContextIDs){ dbgContextIDs=new Dictionary(true); }
			dbgContextIDs[dbgContextID]=true;
		}
		
		public function gotDBGContext(dbgContextID:*):Boolean {
			if(!dbgContextIDs){ return false; }
			return Boolean(dbgContextIDs[dbgContextID]);
		}
		
	}
}
internal class Private {}